package user

import (
	"context"
	// "fmt"
	"net/http"
	// "strconv"
	"strings"

	"mall/service/backend/api/internal/svc"
	"mall/service/backend/api/internal/types"
	"mall/service/backend/common/errorx"
	"mall/service/backend/common/i18n"
	// "mall/service/backend/common/msg/logmsg"
	// "mall/service/backend/rpc/backendclient"

	"github.com/zeromicro/go-zero/core/logx"
	// "github.com/zeromicro/go-zero/core/stores/redis"
)

type GetUserPermCodeLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetUserPermCodeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserPermCodeLogic {
	return &GetUserPermCodeLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetUserPermCodeLogic) GetUserPermCode() (resp *types.PermCodeResp, err error) {
	var roleId string = l.ctx.Value("roleId").(string)
	if roleId == "" {
		return nil, &errorx.ApiError{
			Code: http.StatusUnauthorized,
			Msg:  "login.requireLogin",
		}
	}

	return &types.PermCodeResp{
		BaseDataInfo: types.BaseDataInfo{
			Success: true,
			Msg:     l.svcCtx.Trans.Trans(l.ctx, i18n.Success),
		},
		Data: strings.Split(roleId, ","),
	}, nil
}

// func setRoleInfoToRedis(roleId uint64, rds *redis.Redis, roleInfos []*backendclient.RoleInfo) (err error) {
// 	if _, err := rds.Hget("roleData", strconv.FormatInt(int64(roleId), 10)); err != nil {
// 		for _, v := range roleInfos {
// 			err = rds.Hset("roleData", strconv.FormatInt(int64(*v.Id), 10), *v.Name)
// 			err = rds.Hset("roleData", fmt.Sprintf("%d_code", *v.Id), *v.Code)
// 			err = rds.Hset("roleData", fmt.Sprintf("%d_status", *v.Id), strconv.FormatInt(int64(*v.Status), 10))
// 			if err != nil {
// 				logx.Errorw(logmsg.RedisError, logx.Field("detail", err.Error()))
// 				return errorx.NewCodeInternalError(i18n.RedisError)
// 			}
// 		}
// 	}

// 	return nil
// }
